package com.bit.talkoftoday;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.bit.talkoftoday.ShakeActivity.ImageCreate;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CardActivity extends Activity {

	private String bookName;
	private String contents;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_card);
		Intent intent = getIntent();
		bookName = intent.getStringExtra("bookName");
		contents = intent.getStringExtra("contents");
		
		((TextView)findViewById(R.id.book_name)).setText(bookName);
		((TextView)findViewById(R.id.contents)).setText(contents);
		
		ImageCreate i = new ImageCreate();
		i.start();
	}
	class ImageCreate extends Thread{
		@Override
		public void run() {
			String imgPath=null;
			try {
				sleep(500);
				RelativeLayout v1 = (RelativeLayout)findViewById(R.id.card_layout);
				v1.setDrawingCacheEnabled(true);
				Bitmap bitmap = v1.getDrawingCache();
				
				OutputStream out = null;
				imgPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/rhemacard.jpg";
				try
		        {
		            out = new FileOutputStream(imgPath);
//					Environment.getExternalStorageDirectory()+"/rhemacard.png"
		            bitmap.compress(CompressFormat.JPEG, 100, out);
		            out.flush();
		        }
		        catch (Exception e) 
		        {
		            e.printStackTrace();
		        }
		        finally
		        {
		            try
		            {
		                out.close();
		                bitmap.recycle();
		            }
		            catch (IOException e)
		            {
		                e.printStackTrace();
		            }
		        }
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Intent intent = new Intent();
			intent.setAction("android.appwidget.action.APPWIDGET_CHANGE");
			sendBroadcast(intent);
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.card, menu);
		return true;
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
